<template>
    <div class="about">
        <h1>{{id?'编辑':'新建'}}英雄</h1>
        <el-form label-width="120px" @submit.native.prevent="save">
            <!--value指的是默认在哪个选项卡 -->
            <el-tabs type="border-card" value="basic">
                <el-tab-pane label="基础信息" name="basic">
                    <el-form-item label="名称">
                        <el-input v-model="model.name"></el-input>
                    </el-form-item>
                    <el-form-item label="称号">
                        <el-input v-model="model.title"></el-input>
                    </el-form-item>
                    <el-form-item label="头像">
                        <el-upload
                            class="avatar-uploader"
                            :action="uploadUrl"
                            :headers="getAuthHeaders()"
                            :show-file-list="false"
                            :on-success="res=>$set(model,'avatar',res.url)">  <!--:on-success 成功之后做什么 -->
                            <img v-if="model.avatar" :src="model.avatar" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="Banner">
                        <el-upload
                            class="avatar-uploader"
                            :action="uploadUrl"
                            :headers="getAuthHeaders()"
                            :show-file-list="false"
                            :on-success="res=>$set(model,'banner',res.url)">  <!--:on-success 成功之后做什么 -->
                            <img v-if="model.banner" :src="model.banner" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                    <el-form-item label="类型">
                        <el-select v-model="model.categories" multiple>
                            <el-option v-for="item of categories" :key="item._id"
                            :label="item.name" :value="item._id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="难度">
                        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.difficult"></el-rate>
                    </el-form-item>
                    <el-form-item label="技能">
                        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.skills"></el-rate>
                    </el-form-item>
                    <el-form-item label="攻击">
                        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.attack"></el-rate>
                    </el-form-item>
                    <el-form-item label="生存">
                        <el-rate style="margin-top:0.6rem" :max="9" show-score v-model="model.scores.survive"></el-rate>
                    </el-form-item>
                    <el-form-item label="顺风出装">
                        <el-select v-model="model.items1" multiple>
                            <el-option v-for="item of items" :key="item._id"
                            :label="item.name" :value="item._id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="逆风出装">
                        <el-select v-model="model.items2" multiple>
                            <el-option v-for="item of items" :key="item._id"
                            :label="item.name" :value="item._id"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="使用技巧">
                        <el-input type="textarea" v-model="model.usageTips"></el-input>
                    </el-form-item>
                    <el-form-item label="对抗技巧">
                        <el-input type="textarea" v-model="model.battleTips"></el-input>
                    </el-form-item>
                    <el-form-item label="团战思路">
                        <el-input type="textarea" v-model="model.teamTips"></el-input>
                    </el-form-item>                
                </el-tab-pane>
                <el-tab-pane label="技能" name="skills">
                    <el-button size="small" @click="model.skills.push({})">
                        <i class="el-icon-plus"></i>添加技能
                    </el-button>
                    <el-row type="flex" style="flex-wrap:wrap">
                        <!--一行展示两个宽 -->
                        <el-col :md="12" v-for="(item,i) in model.skills" :key="i">
                            <el-form-item label="名称">
                                <el-input v-model="item.name"></el-input>
                            </el-form-item>
                            <el-form-item label="图标">
                                <el-upload
                                    class="avatar-uploader"
                                    :action="uploadUrl"
                                    :headers="getAuthHeaders()"
                                    :show-file-list="false"
                                    :on-success="res=>$set(item,'icon',res.url)">  
                                    <!--:on-success 成功之后做什么 这里不能用上面的afterUpload了 -->
                                    <!--item.$set(item,'icon',res.url) 用显示复制-->    
                                    <img v-if="item.icon" :src="item.icon" class="avatar">
                                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                </el-upload>
                            </el-form-item>
                            <el-form-item label="冷却值">
                                <el-input v-model="item.delay"></el-input>
                            </el-form-item>
                            <el-form-item label="消耗值">
                                <el-input v-model="item.cost"></el-input>
                            </el-form-item>
                            <el-form-item label="描述">
                                <el-input v-model="item.description" type="textarea"></el-input>
                            </el-form-item>
                            <el-form-item label="小提示">
                                <el-input v-model="item.tips" type="textarea"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button size="small" type="danger" @click="model.skills.splice(i,1)">删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-tab-pane>
                <el-tab-pane label="最佳搭档" name="partners">
                    <el-button size="small" @click="model.partners.push({})">
                        <i class="el-icon-plus"></i>添加英雄 
                    </el-button>
                    <el-row type="flex" style="flex-wrap:wrap">
                        <!--一行展示两个宽 -->
                        <el-col :md="12" v-for="(item,i) in model.partners" :key="i">
                            <el-form-item label="英雄">
                                <el-select filterable v-model="item.hero">
                                    <el-option v-for="hero in heroes" :key="hero._id" :value="hero._id" :label="hero.name"></el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="描述">
                                <el-input v-model="item.description" type="textarea"></el-input>
                            </el-form-item>
                            <el-form-item>
                                <el-button size="small" type="danger" @click="model.partners.splice(i,1)">删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-tab-pane>
            </el-tabs>
            <el-form-item style="margin-top:1rem">
                <el-button type="primary" native-type="submit">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    props:{
        id:{}
    },
    data(){
        return{
            categories:[],
            items:[],
            heroes:[],
            model:{
                skills:[],
                partner:[],
                name:'',
                avatar:'',
                //model.scores.difficult  不把scores写出来会报错，报difficult of undefined
                scores:{
                    difficult:0
                }
            }
        }
    },
    methods: {
        async save(){
            //有ID则用put，没有ID则用post
            if(this.id){
                await this.$http.put(`rest/heroes/${this.id}`,this.model)
            }else{
                await this.$http.post('rest/heroes',this.model)
            }
            this.$router.push('/heroes/list')
            //这个是element提供的方法
            this.$message({
                type:'success',
                message:'保存成功'
            })
        },
        //用来显示当编辑数据时input框里的初始内容
        async fetch(){
            //params会出现在请求头中的querry string parameters中，并且会出现在浏览器的地址栏中，即会拼接到url中。
            const res=await this.$http.get(`rest/heroes/${this.id}`)
            //先把this.model添加到空对象{},然后再把res.data添加到对象里面进去，this.model原先有的scores保证不会被覆盖
            this.model=Object.assign({},this.model,res.data)
        },
        async fetchCategories(){
            //目前调用的分类全部都包括在这里，将来只要英雄的分类就够了
            const res=await this.$http.get(`rest/categories`);
            this.categories=res.data;
        },
        async fetchItems(){
            const res=await this.$http.get(`rest/items`);
            this.items=res.data;
        },
        async fetchHeroes(){
            const res = await this.$http.get(`rest/heroes`)
            this.heroes = res.data
        }
    },
    created(){
        this.fetchCategories()
        this.fetchItems()
        this.id && this.fetch()
        this.fetchHeroes()
    }
}
</script>

<style>

</style>